[์๋ฐ ์น ํ๋ก๊ทธ๋๋ฐ Next Step - ๋ฐ์ฌ์ฑ]์ ์ฝ๊ณ ์ธ์ฉํ๊ณ ์ ๋ฆฌํ๋ POST์ ๋๋ค.
๐ฉ ์ DI๊ฐ ํ์ํ๊ฐ?
๋จผ์ ์์กด๊ด๊ณ(dependency)์ ๋ํด ์๊ฐํด๋ณด์. ๊ฐ์ฒด์๊ฒ ์์กด๊ด๊ณ๋ ๋ฌด์์ด๊ณ ์ด๋ค ๊ฒฝ์ฐ์ ์์กด๊ด๊ณ๊ฐ ๋ฐ์ํ ๊น?
์์กด๊ด๊ณ
: ๊ฐ์ฒด ํผ์ ๋ชจ๋ ์ผ์ ์ฒ๋ฆฌํ๊ธฐ ํ๋ค๊ธฐ์ ์์
์ ๋ค๋ฅธ ๊ฐ์ฒด์๊ฒ ์์ํ๋ฉด์ ๋ฐ์ํ๋ค.
- ์ฆ, ๋ด๊ฐ ๊ฐ์ง ์ฑ ์๊ณผ ์ญํ ์ ๋ค๋ฅธ ๊ฐ์ฒด์๊ฒ ์์ํ๋ ์๊ฐ ๋ฐ์ํ๋ ๊ฒ์ด๋ค.
-
์๋ฅผ ๋ค์ด,
QnaService๋ ์ง๋ฌธ ์ญ์ ๊ธฐ๋ฅ ๊ตฌํ์ ์ํดQuestionDao,AnswerDao์๊ฒ DB ์ ๊ทผ ๋ก์ง์ ์์ํ๊ณ ์๋ค.- ์ด๋
QnaService๊ฐQuestionDao์AnswerDao์ ์์กดํ๊ณ ์๋ค๋ ์๋ฏธ์ด๋ค.
- ์ด๋
DI๋ "๊ฐ์ฒด ๊ฐ ์์กด๊ด๊ณ๋ฅผ ์ด๋ป๊ฒ ํด๊ฒฐํ๋๋"์ ๋ฐ๋ฅธ ์๋ก์ด ์ ๊ทผ ๋ฐฉ์์ด๋ค.
- ์ด์ ๊น์ง๋ ์์กด๊ด๊ณ์ ์๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ๊ณ , ์ฌ์ฉํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ค. (
QnaService๋ํQuestionDao์AnswerDao์ฌ์ฉ์ ์ํด ์์ฑํ๊ณ ์ฌ์ฉํ๋ค.)
ํ์ง๋ง ์ด๊ฐ์ ๋ฐฉ์์ผ๋ก ๊ตฌํ ์ ์ ์ฐํ ๊ฐ๋ฐ์ ํ๊ณ๊ฐ ์๊ธฐ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ์ฑ ์๊ณผ ์ฌ์ฉํ๋ ์ฑ ์์ ๋ถ๋ฆฌํ์๋ ๊ฒ์ด DI์ ํต์ฌ์ด๋ค.
- ์ฆ ์ ์์์
QnaService๋QuestionDao์AnswerDao์ ๋ํ ์์ฑ ์์ด ์ฌ์ฉ๋ง ํจ์ผ๋ก์จ ์ ์ฐ์ฑ์ ๋์ด์๋ ๋ฐฉ์์ด๋ค.
๊ทธ๋ผ ์ง๊ธ๊น์ง์ ๊ตฌํ ๋ฐฉ์์ ๋ฌธ์ ์ ์ ๊ฐ๋จํ ์์ ๋ก ์ดํด๋ณด๋๋ก ํ๋ค.
public class DateMessageProvider {
public String getDateMessage() {
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
if (hour < 12) {
return "์ค์ ";
}
return "์คํ";
}
}
- ํ์ฌ ์์คํ
์๊ฐ์ ๋ฐ๋ผ ์ค์ ๋๋ ์คํ๋ฅผ ๋ฐํํ๋ ํด๋์ค์ด๋ค. ์ด ํด๋์ค์
getDateMessage()์ ๋ํ ํ ์คํธ ํด๋์ค๋ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌํํ ์ ์๋ค.
public class DateMessageProviderTest {
@Test
public void ์ค์ () throws Exception {
DateMessageProvider provider = new DateMessageProvider();
assertEquals("์ค์ ", provider.getDateMessage());
}
@Test
public void ์คํ() throws Exception {
DateMessageProvider provider = new DateMessageProvider();
assertEquals("์คํ", provider.getDateMessage());
}
}
-
์ ํ ์คํธ๋ฅผ ์คํํ๋ฉด, ๋ฐ๋์ ํ๋์ ํ ์คํธ๋ ์คํจํ๋ค. ์๊ตฌ์ฌํญ์ ์ด ๋ ํ ์คํธ๊ฐ ๋ชจ๋ ์ฑ๊ณตํ๋๋ก ๋ฆฌํฉํ ๋งํ๋ ๊ฒ์ด๋ค.
- ์ด๋ฅผ ์ํด์๋ ๋จผ์ ํ ์คํธ๊ฐ ๋ชจ๋ ์ฑ๊ณตํ์ง ๋ชปํ๋ ์์ธ์ ์์์ผ ํ ๊ฒ์ด๋ค.
์์ธ์, DateMessageProvider ๊ฐ Calendar ์ ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง๋๋ฐ ํ
์คํธ๋ฅผ ์ํด Calendar ์ ์๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค.
-
์์ค์ฝ๋๋ฅผ ์ปดํ์ผํ๋ ์์ ์
Calendar์ธ์คํด์ค๊ฐ ์ด๋ฏธ ๊ฒฐ์ ๋์ด ๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์ด๋ค.- ์ด๊ฐ์ ์์กด๊ด๊ณ๋ฅผ ๊ฐํ๊ฒ ๊ฒฐํฉ(tightly coupling)๋์ด ์๋ค๊ณ ํ๋ค.
-
๋ชจ๋ ํ ์คํธ์ ์ฑ๊ณต์ ์ํด์๋
Calendar์์ ๋ฐํ๋๋ ์๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋๋กDateMessageProvider๋ฅผ ๋ฆฌํฉํ ๋งํด์ผ ํ๋ค.- ์ฆ,
getDateMessage()๊ฐ ์๋DateMessageProvider์ธ๋ถ์์Calendar์ธ์คํด์ค ์์ฑ ํ ์ ๋ฌํ๋ ๊ตฌ์กฐ๋ก ๋ฐ๊ฟ์ผ ํ๋ค. - ์์ฑ์๋ฅผ ํตํด ์ ๋ฌํ๊ฑฐ๋,
getDateMessage()๋ฉ์๋ ์ธ์๋ก ์ ๋ฌํ ์ ์๋ค. (๋๋ฒ์งธ ๋ฐฉ๋ฒ ์ฌ์ฉ)
- ์ฆ,
public class DateMessageProvider {
public String getDateMessage(Calendar now) {
int hour = now.get(Calendar.HOUR_OF_DAY);
if (hour < 12) {
return "์ค์ ";
}
return "์คํ";
}
}
public class DateMessageProviderTest {
@Test
public void ์ค์ () throws Exception {
DateMessageProvider provider = new DateMessageProvider();
assertEquals("์ค์ ", provider.getDateMessage(createCurrentDate(11)));
}
@Test
public void ์คํ() throws Exception {
DateMessageProvider provider = new DateMessageProvider();
assertEquals("์คํ", provider.getDateMessage(createCurrentDate(13)));
}
public Calendar createCurrentDate(int hourOfDay) {
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY, hourOfDay);
return now;
}
}
์ด์ ๋ชจ๋ ํ ์คํธ๊ฐ ์ฑ๊ณต์ด๋ค.
DateMessageProvider๊ฐ ์์กดํ๊ณ ์๋Calendar์ธ์คํด์ค์ ์์ฑ์DateMessageProvider๊ฐ ๊ฒฐ์ ํ์ง ์๊ณ , ์ธ๋ถ๋ก๋ถํฐ ์ ๋ฌ๋ฐ์์ผ๋ก์จ ์ํ๋ ์๊ฐ์ผ๋ก ๋ณ๊ฒฝํด ๋ชจ๋ ํ ์คํธ๋ฅผ ์ฑ๊ณตํ ์ ์์๋ค.- ์ด์ฒ๋ผ DI๋ ๊ฐ์ฒด ๊ฐ์ ์์กด๊ด๊ณ์ ๋ํ ๊ฒฐ์ ๊ถ์, ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง๋ ๊ฐ์ฒด(
DateMessageProvider)๊ฐ ๊ฐ์ง๋ ๊ฒ์ด ์๋ ์ธ๋ถ์ ๋๊ตฐ๊ฐ๊ฐ ๋ด๋นํ๋๋ก ๋งก๊ฒจ ๋ ์ ์ฐํ ๊ตฌ์กฐ๋ก ๊ฐ๋ฐํ๋ค.
๐ฉ DI๋ฅผ ์ ์ฉํ๋ฉด์ ์์ด๋ ๋ถํธํจ ๋๋ ๋ถ๋ง
์ง๋ฌธ/๋ต๋ณ ๊ฒ์ํ์ QnaService ๋ฅผ DI๊ธฐ๋ฐ์ผ๋ก ๋ณ๊ฒฝํ๋ฉด์ ๊ธฐ์กด์ ๋ฐฉ์๊ณผ ์ด๋ป๊ฒ ๋ค๋ฅด๊ณ , ์ด๋ค ๋ถํธํจ์ด ์๋์ง ์ดํด๋ณธ๋ค.
public class QnaService {
private static QnaService qnaService;
private QuestionDao questionDao = QuestionDao.getInstance();
private AnswerDao answerDao = AnswerDao.getInstance();
private QnaService() {
}
public static QnaService getInstance() {
if (qnaService == null) {
qnaService = new QnaService();
}
return qnaService;
}
...
}
- ๋ณด์ด๋ ๊ฒ๊ณผ ๊ฐ์ด
QuestionDao์AnswerDao์ ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง๋ค. ์ด๋ฅผ DI๊ตฌ์กฐ๋ก ๋ณ๊ฒฝํด๋ณธ๋ค.
public class QnaService {
private static QnaService qnaService;
private QuestionDao questionDao;
private AnswerDao answerDao;
private QnaService(QuestionDao questionDao, AnswerDao answerDao) {
this.questionDao = questionDao;
this.answerDao = answerDao;
}
public static QnaService getInstance(QuestionDao questionDao, AnswerDao answerDao) {
if (qnaService == null) {
qnaService = new QnaService(questionDao, answerDao);
}
return qnaService;
}
...
}
- ์์์ ๋ค๋ฃฌ
DateMessageProvider์์ ์์๋getDateMessage()์์๋ง ๋ฐ์ํ๋ ์์กด๊ด๊ณ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ฉ์๋ ์ธ์๋ก ์ ๋ฌํ๋ค. -
QnaService์QuestionDao,AnswerDao๋QnaService๋ฉ์๋ ์ ์ฒด์์ ์ฌ์ฉ๋๊ธฐ์ ์์ฑ์๋ฅผ ํตํด ํ๋๋ก ๊ด๋ฆฌํ๋๋ก ๋ณ๊ฒฝํ๋ค.getInstance()๋ฉ์๋๋ฅผ ๋ณ๊ฒฝํ๊ธฐ์ ์ด๋ฅผ ์ฌ์ฉํ๋ ๋ค๋ฅธ ์ปจํธ๋กค๋ฌ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ์ด๋ฅผprivate QnaService qnaService = QnaService.getInstance(QuestionDao.getInstance(), AnswerDao.getInstance());์ ๊ฐ์ด ๊ณ ์ณ์ค๋ค.
์ด์ ํ
์คํธ๋ฅผ ์ํํ ํ
๋ฐ, QuestionDao ์ AnswerDao ๊ฐ DB์์ ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง๊ธฐ์ ์ด๋ฅผ ๊ฐ์ง์ง ์๋๋ก ๋ณ๊ฒฝํ ์ ์์ด์ผ DB์ ์์กดํ์ง ์๋ ํ
์คํธ๊ฐ ๊ฐ๋ฅํ๋ค.
-
QuestionDao์AnswerDao๋ฅผ ์์ํด ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ์ฌ ํด๊ฒฐํ๋ค.- ๋จผ์
MockQuestionDaoํด๋์ค๋ฅผ ์ถ๊ฐํ๊ณQuestionDao๋ฅผ ์์ํ๋ค. - ์ด๋,
QuestionDao์ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ์ฑ๊ธํค ํจํด์ ์ ์ฉ์ ์ํดprivate์ผ๋ก ๊ตฌํํ์ฌ ์์ํ ์ ์๋ค.
- ๋จผ์
๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก, ๊ฐ์ฒด ๊ฐ์ ์์กด๊ด๊ณ์ ๋ํ ๊ฐํ ๊ฒฐํฉ(tightly coupling)์ ์ค์ด๊ธฐ ์ํด DI๋ฅผ ์ ์ฉํ๋ฉด์ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ค.
QuestionDaoโJdbcQuestionDaorenamingAnswerDaoโJdbcAnswerDaorenaming- ์ธํฐํ์ด์ค ์ด๋ฆ์
QuestionDao,AnswerDao๋ก ์ฌ์ฉ JdbcQuestionDao,JdbcAnswerDao์ ์์กดํ๊ณ ์๋QnaService์ฝ๋๋ฅผ ์๋ก ์ถ๊ฐํ ์ธํฐํ์ด์ค ๊ฐ๊ฐ์ ์์กดํ๋๋ก ๋ณ๊ฒฝMockQuestionDao,MockAnswerDao๋ฅผ ๊ตฌํ
public class MockAnswerDao implements AnswerDao {
private Map<Long, Answer> answers = Maps.newHashMap();
@Override
public Answer insert(Answer answer) {
return answers.put(answer.getAnswerId(), answer);
}
@Override
public Answer findById(long answerId) {
return answers.get(answerId);
}
@Override
public List<Answer> findAllByQuestionId(long questionId) {
return answers.values().stream()
.filter(a -> a.getQuestionId() == questionId)
.collect(Collectors.toList());
}
@Override
public void delete(Long answerId) {
answers.remove(answerId);
}
}
MockQuestionDao๋ํ ์์ ๋์ผํ๊ฒ ๊ฐ๋จํ ๋ฉ๋ชจ๋ฆฌ DB ์ญํ ์ ํ๋๋ก ๊ตฌํํ๋ค.- ์ด์ ํ ์คํธ ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ค.
public class QnaServiceTest {
private QnaService qnaService;
private MockQuestionDao questionDao;
private MockAnswerDao answerDao;
@Before
public void setup() {
questionDao = new MockQuestionDao();
answerDao = new MockAnswerDao();
qnaService = QnaService.getInstance(questionDao, answerDao);
}
@Test(expected = CannotDeleteException.class)
public void deleteQuestion_์๋_์ง๋ฌธ() throws Exception {
qnaService.deleteQuestion(1L, newUser("userId"));
}
@Test(expected = CannotDeleteException.class)
public void deleteQuestion_๋ค๋ฅธ_์ฌ์ฉ์() throws Exception {
Question question = newQuestion(1L, "javajigi");
questionDao.insert(question);
qnaService.deleteQuestion(1L, newUser("userId"));
}
@Test
public void deleteQuestion_๊ฐ์_์ฌ์ฉ์_๋ต๋ณ์์() throws Exception {
Question question = newQuestion(1L, "javajigi");
questionDao.insert(question);
qnaService.deleteQuestion(1L, newUser("javajigi"));
}
}
newUser์newQuestion๋ฉ์๋๋model์ ๊ฐ ํด๋์ค ํ ์คํธ ์ฝ๋์ ๊ตฌํํ๋ค.-
์ ํ ์คํธ์์
deleteQuestion_๊ฐ์_์ฌ์ฉ์_๋ต๋ณ์์()๋ง "์กด์ฌํ์ง ์๋ ์ง๋ฌธ์ ๋๋ค." ๋ผ๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ๋ถ๋ช ์ง๋ฌธ์ ์ถ๊ฐํ๋๋ฐ๋ ์ด๋ฌํ ์๋ฌ๊ฐ ๋ฌ๋ค.- ์ด๋
QnaService๊ฐ ์ฑ๊ธํค ์ธ์คํด์ค์ด๊ธฐ์ ๋ฐ์ํ๋ ์๋ฌ์ด๋ค. - ์ฆ,
QnaService๊ฐ ์ฐธ์กฐํ๋QuestionDao(์ฒ์ ์ธ์คํด์ค ์์ฑ ์ ํด๋น ์ธ์คํด์ค)์ ํ ์คํธ ๋ฉ์๋์์ ์์ฑํ ์ธ์คํด์ค๊ฐ ๋ค๋ฅด๋ค๋ ๊ฒ์ด๋ค. - ์ด๋ ํ
์คํธ ๋ฐ์ดํฐ๋ฅผ ์ด๊ธฐํํ๋ ์์
์
setup()๋ฉ์๋์์๋ง ํ๋๋ก ๋ณ๊ฒฝํด ํด๊ฒฐํ ์ ์๋ค.
- ์ด๋
๐ฉ ๋ถ๋ง ํด์ํ๊ธฐ
- ์ฑ๊ธํค ํจํด์ ์ฌ์ฉํจ์ผ๋ก ์ธํด ํ ์คํธ์ ์ด๋ ค์์ด ์์๋ค. ์ด๋ค ํํ๋ก๋ ํ ์คํธํ ์ ์์ง๋ง ๊ณ ๋ คํด์ผ ํ ๋ถ๋ถ์ด ๋ง์์ง๋ค๋ ๊ฒ์ ๋ฐ๊ฐ์ง ์๋ค.
- ํ
์คํธ๋ฅผ ์ํด ๋งค๋ฒ
Mock๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๊ฒ์ ๋ง์ ๋น์ฉ์ด ๋๋ ์์ ์ด๋ค.
๐ง ์ฑ๊ธํค ํจํด์ ์ ๊ฑฐํ DI
์ด ๋์์ธ ํจํด์ ์ดํดํ๊ธฐ๋ ๊ฐ์ฅ ์ฝ๊ธฐ์ ๋๋ฆฌ ์ฌ์ฉ๋์๋ค. ํ์ง๋ง ๊ทธ์ ๋ฐ๋ฅธ ๋จ์ ๋ ๋ง๋ค.
- ์ฑ๊ธํค ํจํด์ผ๋ก ๊ตฌํ๋ ํด๋์ค์ ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง๋ ๊ฒฝ์ฐ ํด๋น ํด๋์ค์ ๊ฐํ ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ํ ์คํธํ๊ธฐ ์ด๋ ต๊ณ ,
- ์์ฑ์๋ฅผ
private๋ก ๊ตฌํํด ์์ํ ์ ์๋ค. - ๋ํ ๊ฐ์ฒด์งํฅ ์ค๊ณ ์์น์ ๋ฐ๋ผ ๊ฐ๋ฐํ๋ ๊ฒ์ ์ ํดํ๋ ์์ธ์ด ๋๋ค.
์ฑ๊ธํค ํจํด์ ์ฌ์ฉํ์ง ์์ผ๋ฉด์ ์ธ์คํด์ค ํ๋๋ง ์ ์งํ๋ ๋ฐฉ๋ฒ์ด ์์๊น?
- ์ง๊ธ๊น์ง ์ธ์คํด์ค ํ๋๋ง ์์ฑํ๊ธฐ ์ํด
Service,DAO,JdbcTemplate์ ์ฑ๊ธํค ํจํด์ ์ ์ฉํ๋ค. -
ํ์ง๋ง ์ปจํธ๋กค๋ฌ๋ ์ด๋ฅผ ์ ์ฉํ์ง ์๊ณ ๋ ๊ฐ์ ํจ๊ณผ๋ฅผ ๋ผ ์ ์์๋ค.
- ์๋ธ๋ฆฟ ์ปจํ
์ด๋๊ฐ
DispatcherServlet์ ์ด๊ธฐํํ๋ ์์ ์ ์ปจํธ๋กค๋ฌ ์ธ์คํด์ค๋ฅผ ์์ฑํ ํ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ๋๋ก ๊ตฌํํ๊ธฐ ๋๋ฌธ์ด๋ค. - ๊ทธ๋ ๋ค๋ฉด, ์ด ๋ฐฉ์์ผ๋ก ๋ค๋ฅธ ํด๋์ค๋ ์ธ์คํด์ค๋ฅผ ๊ด๋ฆฌํ๋ค๋ฉด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ง ์์๊น? ๊ทธ๋ฆฌ๊ณ ๊ฐ ์ธ์คํด์ค ๊ฐ ์์กด๊ด๊ณ๋ DI ๊ธฐ๋ฐ์ผ๋ก ๊ตฌํํ๋ค.
- ์๋ธ๋ฆฟ ์ปจํ
์ด๋๊ฐ
๋จผ์ QnaService ์ QuestionDao ์ AnswerDao ๋ฅผ ์ด ๋ฐฉ๋ฒ์ผ๋ก ๊ตฌํํ๊ณ ํ
์คํธ๋ฅผ ์งํํ๋ค.
- ์ฑ๊ธํค ํจํด ์ฝ๋๋ฅผ ์ ๊ฑฐํ๊ณ ์์ฑ์๋ฅผ
public์ผ๋ก ๋ณ๊ฒฝํ๋ค.
public class QnaService {
private QuestionDao questionDao;
private AnswerDao answerDao;
public QnaService(QuestionDao questionDao, AnswerDao answerDao) {
this.questionDao = questionDao;
this.answerDao = answerDao;
}
...
}
- ์์ ๊ฐ์ด ์์ ํ
getInstance()๋ฉ์๋๋ฅผ ์ ๊ฑฐํ๊ณ ํ ์คํธ๋ฅผ ์งํํ๋ฉด ์ ์์ ์ผ๋ก ๋์ํ๋ค. ์ด์ QnaService์ ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง๋ ๋ค๋ฅธ ์ปจํธ๋กค๋ฌ๋ค์ ์์ ํ๋ค.
public class DeleteQuestionController extends AbstractController {
private QnaService qnaService;
public DeleteQuestionController(QnaService qnaService) {
this.qnaService = qnaService;
}
...
}
- ์์ฒ๋ผ,
public์์ฑ์๋ฅผ ๋ง๋ค์ด ์ธ์๋ก ๋ฐ์QnaService์ธ์คํด์ค๋ฅผ ์ฌ์ฉํ๋๋ก ์์ ํ๋ค. - ์ด์ ์ด ์ปจํธ๋กค๋ฌ๋ค์ ์์ฑํ ๋
QnaService๋ฅผ DI๋ก ์ ๋ฌํด์ค๋ค.
public class LegacyHandlerMapping implements HandlerMapping {
private static final Logger logger = LoggerFactory.getLogger(DispatcherServlet.class);
private Map<String, Controller> mappings = new HashMap<>();
public void initMapping() {
QnaService qnaService = new QnaService(JdbcQuestionDao.getInstance(), JdbcAnswerDao.getInstance());
...
mappings.put("/qna/delete", new DeleteQuestionController(qnaService));
mappings.put("/api/qna/deleteQuestion", new ApiDeleteQuestionController(qnaService));
...
logger.info("Initialized Request Mapping!");
}
...
}
-
๋ค์ ๋ณด๋,
JdbcQuestionDao,JdbcAnswerDao๋ ์์ง ์ฑ๊ธํค ํจํด์ ์ฌ์ฉํ๊ณ ์๋ค. ์ด๋ค๋ ์ฑ๊ธํค ํจํด์ ์ ์ฉํ์ง ์๋๋ก ์์ ํ๋ค. ๊ทธ๋ฆฌ๊ณ ์ฐ์ด์ด ๋ฐ์ํ๋ ์ปจํธ๋กค๋ฌ์์์ ์ปดํ์ผ ์๋ฌ๋ ๋ชจ๋ ์์ ํ๋ค.getInstance()๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๋ฅผ ๋ชจ๋ ์ ๊ฑฐํ๊ณ ,public์์ฑ์์ ์ธ์๋ก ํ์ํ ์ธ์คํด์ค๋ฅผ ๋ฐ์ ์ฌ์ฉํ๋๋ก ์์ ํ๊ณ , ๋งคํ ์ด๊ธฐํ ์ ํ์ํ ์ธ์คํด์ค๋ฅผ ๋ชจ๋ ์ ๋ฌํด์ฃผ๋๋ก ํ๋ค.
๐ง Mockito๋ฅผ ํ์ฉํ ํ ์คํธ
DB๊ฐ ์๋ ์ํ์์๋ ํ
์คํธ๊ฐ ๊ฐ๋ฅํ๋๋ก QuestionDao ์ AnswerDao ์ ๋ํ ๊ฐ์ง(Mock) ํด๋์ค๋ฅผ ๊ตฌํํ๋ค. ํ์ง๋ง ์ด๋ฐ ๋ฐฉ์์ ํ
์คํธ ์ฝ๋ ๊ตฌํ๋ ๋ถ๋ด์ธ๋ฐ ๋งค๋ฒ ์๋ก์ด ๊ฐ์ง ํด๋์ค๋ฅผ ๋ง๋ค์ด์ผ ํ๋ ๋ถ๋ด์ด ์ถ๊ฐ๋๋ ๊ฒฉ์ด๋ค.
- ์ด๋ฅผ ์ํด
Mockํด๋์ค ๊ตฌํ ์์ด๋ ํ ์คํธ๊ฐ ๊ฐ๋ฅํ๋๋ก ์ง์ํ๋Mockํ ์คํธ ํ๋ ์์ํฌ๋ฅผ ํ์ฉํ๋ค.
๋จผ์ ๋ฉ์ด๋ธ ์ค์ ํ์ผ์ ์์กด๊ด๊ณ๋ฅผ ์ถ๊ฐํ๋ค.
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
์ด์ ์ด๋ฅผ ํ์ฉํด QnaService ์ deleteQuestion() ๋ฉ์๋์ ๋ํ ํ
์คํธ ์ฝ๋๋ฅผ ์์ ํ๋ค.
@RunWith(MockitoJUnitRunner.class)
public class QnaServiceTest {
@Mock
private QuestionDao questionDao;
@Mock
private AnswerDao answerDao;
private QnaService qnaService;
@Before
public void setup() {
qnaService = new QnaService(questionDao, answerDao);
}
@Test(expected = CannotDeleteException.class)
public void deleteQuestion_์๋_์ง๋ฌธ() throws Exception {
when(questionDao.findById(1L)).thenReturn(null);
qnaService.deleteQuestion(1L, newUser("userId"));
}
...
@Test
public void deleteQuestion_๊ฐ์_์ฌ์ฉ์_๋ต๋ณ์์() throws Exception {
Question question = newQuestion(1L, "javajigi");
when(questionDao.findById(1L)).thenReturn(question);
when(answerDao.findAllByQuestionId(1L)).thenReturn(Lists.newArrayList());
qnaService.deleteQuestion(1L, newUser("javajigi"));
}
}
-
Mockํด๋์ค ์์ด ํ ์คํธ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์๋ฃํ๋ค.- Mockito๋
Mockํด๋์ค ๋์@Mockannotation์ผ๋ก ์ค์ ํ ํด๋์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ์ ๋, ๋ฐํ ๊ฐ์ ์ง์ ํ ์ ์๋ค. - ๋ํ ํด๋น annotation์ ์ค์ ํ ํด๋์ค์ ๋ฉ์๋๊ฐ ํธ์ถ๋๋์ง ์ฌ๋ถ๋ฅผ
verify()๋ฉ์๋๋ฅผ ํตํด ๊ฒ์ฆํ๋ ์์ ๋ํ ๊ฐ๋ฅํ๋ค. - ์ด์ฒ๋ผ ๋ค๋ฅธ ํด๋์ค์ ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง๋ ํด๋์ค๋ฅผ ํ
์คํธํ๋ ๊ฒฝ์ฐ
Mockํ๋ ์์ํฌ์ ์ฌ์ฉ์ ์ถ์ฒํ๋ค.
- Mockito๋
๐ง DI๋ณด๋ค ์ฐ์ ํ๋ ๊ฐ์ฒด์งํฅ ๊ฐ๋ฐ
"๊ณ์ธตํ ์ํคํ ์ฒ ๊ด์ ๊ณผ ๊ฐ์ฒด์งํฅ ์ค๊ณ ๊ด์ ์์ ํต์ฌ์ ์ธ ๋น์ฆ๋์ค ๋ก์ง์ ๊ตฌํํด์ผ ํ๋ ์ญํ ์ ๋๊ฐ ๋ด๋นํด์ผํ ๊น?"
- ๋จผ์ ์์์ ๊ตฌํํ
DateMessageProvider์ฝ๋๋ฅผ ๋ค์ ๋ณด๋ฉด์ ๋ต์ ๋ํด ์๊ฐํด๋ณธ๋ค. - ์ด ์ฝ๋๋ฅผ ์ํ ํ ์คํธ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
public class DateMessageProviderTest {
@Test
public void ์ค์ () throws Exception {
DateMessageProvider provider = new DateMessageProvider();
assertEquals("์ค์ ", provider.getDateMessage(createCurrentDate(11)));
}
@Test
public void ์คํ() throws Exception {
DateMessageProvider provider = new DateMessageProvider();
assertEquals("์คํ", provider.getDateMessage(createCurrentDate(13)));
}
public Calendar createCurrentDate(int hourOfDay) {
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY, hourOfDay);
return now;
}
}
-
์ด์ฒ๋ผ ๊ฐ๋จํ ๋ก์ง์ ๊ตฌํํ๋๋ฐ ์ข ๋ ๊ฐ๋จํ ํ ์คํธํ ์ ์๋ ๋ฐฉ๋ฒ์ ์์๊น์ ๋ํด ์๊ฐํด๋ณผ ์ ์๋ค.
DateMessageProvider์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ ํด ํ ์คํธ๋ฅผ ๋ ์ฝ๊ฒ ํ ์ ์์๊น?- ์ข ๋ ๊ฐ์ฒด์งํฅ์ ์ธ ๊ฐ๋ฐ์ ์ํํ๋ค๋ฉด ํด๊ฒฐ์ฑ ์ ์ป์ ์ ์์ ๊ฒ์ด๋ค.
๋จผ์ ์๊ฐ์ ์ถ์ํํ๋ Hour ํด๋์ค๋ฅผ ์ถ๊ฐํ๋ค.
public class Hour {
private int hour;
public Hour(int hour) {
this.hour = hour;
}
public String getMessage() {
if (hour < 12) {
return "์ค์ ";
}
return "์คํ";
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
return false;
if (getClass() != obj.getClass()) {
return false;
}
Hour other = (Hour) obj;
if (hour != obj.hour) {
return false;
}
return true;
}
}
Hourํด๋์ค๊ฐDateMessageProvider๊ฐ ๊ตฌํํ๋ ๋ก์ง์ ๋ด๋นํ๊ฒ ๋์๋ค. ํ ์คํธ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
public class MyCalendarTest {
@Test
public void getHour() throws Exception {
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY, 11);
MyCalendar calendar = new MyCalendar(now);
assertEquals(new Hour(11), calendar.getHour());
}
}
์์์ ๋งํ ์ง๋ฌธ์ ๋ํ ๋ต๋ณ์ผ๋ก, ๋๋ถ๋ถ Service layer๋ฅผ ๋ด๋นํ๊ณ ์๋ QnaService ์์ ์ฒ๋ฆฌํด์ผ ํ๋ค๊ณ ์๊ฐํ๋ค. ํ์ง๋ง ํต์ฌ์ ์ธ ๋น์ฆ๋์ค ๋ก์ง ๊ตฌํ์ Domain ๊ฐ์ฒด๊ฐ ๋ด๋นํ๋ ๊ฒ์ด ๋ง๋ค.
- Service layer์ ํต์ฌ ์ญํ ์ Domain ๊ฐ์ฒด๋ค์ด ๋น์ฆ๋์ค ๋ก์ง์ ๊ตฌํํ ์ ์๋๋ก Domain ๊ฐ์ฒด๋ฅผ ์กฐํฉํ๊ฑฐ๋, ๋ก์ง ์ฒ๋ฆฌ ์๋ฃ ์ ์ํ ๊ฐ์ DAO๋ฅผ ํ์ฉํด DB์ ์๊ตฌ ์ ์ฅํ๋ ๋ฑ์ด๋ค.
๋๋ถ๋ถ์ ๊ฐ๋ฐ์๋ค์ QnaService ์ deleteQuestion() ๋ฉ์๋์ ๊ฐ์ด ๊ตฌํํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค. ์ด๋ ๋ํ์ ์ธ ์ ์ฐจ์งํฅ์ ๊ฐ๋ฐ์ด๋ค.
- ์ด์ ๊ฐ์ ๊ฐ๋ฐ์ Service layer์ ๋ณต์ก๋๋ ์ ์ ๋ ์ฆ๊ฐํ๊ณ
- ์ ์ง๋ณด์์ ํ ์คํธํ๊ธฐ ํ๋ ์ํฉ์ ๋ง๋ฅ๋จ๋ฆฌ๊ฒ ๋๋ค.
-
๋ํ ํต์ฌ ๊ฐ์ฒด๋ผ ํ ์ ์๋ Domain ๊ฐ์ฒด๋ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋ฐ์ดํฐ๋ฅผ DAO์ ์ ๋ฌํ๊ฑฐ๋ DB ๋ฐ์ดํฐ๋ฅผ ๋ทฐ์ ์ ๋ฌํ๋ ์ญํ ๋ง์ ์ํํ๊ฒ ๋๋ค. (ํต์ฌ์ ์ธ ์ญํ ์ํ X)
- ๊ทธ๋ฆฌํ์ฌ Domain ๊ฐ์ฒด๊ฐ ๊ฐ์ ์ ๋ฌํ๋
Getter&Setter๋ฉ์๋๋ง์ ๊ฐ์ง๋ ์ํฉ์ด ๋ฐ์ํ๋ค.
- ๊ทธ๋ฆฌํ์ฌ Domain ๊ฐ์ฒด๊ฐ ๊ฐ์ ์ ๋ฌํ๋
์ด์ ๋ถํฐ๋ Domain ๊ฐ์ฒด์ ๋ ๋ง์ ์ญํ ์ ๋ถ์ฌํ๋ ๋ฐฉํฅ์ผ๋ก ๊ตฌํํ๋๋ก ํ๋ค.
QnaService์deleteQuestion()๋ฉ์๋๊ฐ ๊ตฌํํ๊ณ ์๋ ๋ก์ง์ Domain ๊ฐ์ฒด, ์ฆQuestion&Answer๊ฐ ๋ด๋นํ๋๋ก ํ๋ค.- ๋จผ์ , ์ญ์ ๊ฐ๋ฅ ์ฌ๋ถ๋ฅผ
Question์ ๋ฉ์ธ์ง๋ฅผ ๋ณด๋ด(canDelete()) ํ์ธํ๋ ๋ฐฉ์์ผ๋ก ๋ฆฌํฉํ ๋งํ๋ค.
public class Question {
...
public boolean canDelete(User user, List<Answer> answers) throws CannotDeleteException {
if (!user.isSameUser(this.writer)) {
throw new CannotDeleteException("๋ค๋ฅธ ์ฌ์ฉ์๊ฐ ์์ฑํ ๊ธ์ ์ญ์ ํ ์ ์์ต๋๋ค.");
}
for (Answer answer : answers) {
if (!answer.canDelete(user)) {
throw new CannotDeleteException("๋ค๋ฅธ ์ฌ์ฉ์๊ฐ ์ถ๊ฐํ ๋๊ธ์ด ์กด์ฌํด ์ญ์ ํ ์ ์์ต๋๋ค.");
}
}
return true;
}
}
-
Question์ ์ธ์๋ก ์ ๋ฌ๋User,Answer์ ์ญํ ์ ๋๋ ์ญ์ ๊ฐ๋ฅ ์ฌ๋ถ๋ฅผ ํ๋จํ๋ค.User์ ์ง๋ฌธํ ์ฌ๋์ ID๋ฅผ ์ ๋ฌํด ์ฌ์ฉ์๊ฐ ๋์ผํ์ง ํ๋จํ๊ณ ,- ๊ธ์ด์ด์ ๋ก๊ทธ์ธ ์ฌ์ฉ์๊ฐ ๊ฐ์ ๊ฒฝ์ฐ ๊ฐ ๋ต๋ณ์ด ์ญ์ ๊ฐ๋ฅํ ์ํ์ธ์ง ํ์ธํ๋ค. (
Answer)
public class User {
...
public boolean isSameUser(String newUserId) {
return userId.equals(newUserId);
}
}
public class Answer {
...
public boolean canDelete(User user) {
return user.isSameUser(this.writer);
}
}
์ด์ ์ด๋ฅผ ํ์ฉํด deleteQuestion() ์ ์์ ํ๋ค.
public class QnaService {
...
public void deleteQuestion(long questionId, User user) throws CannotDeleteException {
Question question = questionDao.findById(questionId);
if (question == null) {
throw new CannotDeleteException("์กด์ฌํ์ง ์๋ ์ง๋ฌธ์
๋๋ค.");
}
List<Answer> answers = answerDao.findAllByQuestionId(questionId);
if (question.canDelete(user, answers)) {
questionDao.delete(questionId);
}
}
}
- Domain ๊ฐ์ฒด๋ก ๋ก์ง์ ์ด๋์์ผ ๋งค์ฐ ๊น๋ํด์ก๋ค. ๊ทธ๋ ๋ค๊ณ Domain ๊ฐ์ฒด์ ๋ณต์กํ ๋ก์ง์ด ๊ตฌํ๋ ๊ฒ์ ์๋๋ค.
"๊ฐ์ฒด์งํฅ ๊ฐ๋ฐ์ ํ๊ธฐ ์ํ ์ข์ ์ฐ์ต์ ์ํ ๊ฐ์ ๊ฐ์ง๋ Domain ๊ฐ์ฒด์์ ๊ฐ์ ๊บผ๋ด๋ ค๊ณ ํ์ง ๋ง๊ณ ๊ฐ์ฒด์ ๋ฉ์ธ์ง๋ฅผ ๋ณด๋ด ์์ ์ ์์ํ๋ค๋ ์๊ฐ์ผ๋ก๋ถํฐ ์์ํ๋ค."
๊ฐ์ฒด์งํฅ์ ๊ฐ๋ฐ ์ ์ฅ์ ์ค ํ๋๋ ํ
์คํธํ๊ธฐ ์ฝ๋ค๋ ๊ฒ์ด๋ค. ์๋๋ Question ์ canDelete() ๋ฉ์๋์ ๋ํ ํ
์คํธ ์ฝ๋์ด๋ค.
public class QuestionTest {
public static Question newQuestion(String writer) {
return new Question(1L, writer, "title", "contents", new Date(), 0);
}
public static Question newQuestion(long questionId, String writer) {
return new Question(questionId, writer, "title", "contents", new Date(), 0);
}
@Test(expected = CannotDeleteException.class)
public void canDelete_๋ค๋ฅธ_๊ธ์ด์ด() throws Exception {
User user = newUser("javajigi");
Question question = newQuestion("sanjigi");
question.canDelete(user, new ArrayList<Answer>());
}
@Test
public void canDelete_๊ธ์ด์ด_๊ฐ์_๋ต๋ณ_์์() throws Exception {
User user = newUser("javajigi");
Question question = newQuestion("javajigi");
assertTrue(question.canDelete(user, new ArrayList<Answer>()));
}
@Test
public void canDelete_๊ฐ์_์ฌ์ฉ์_๋ต๋ณ() throws Exception {
User user = newUser("javajigi");
Question question = newQuestion("javajigi");
List<Answer> answers = Arrays.asList(newAnswer("javajigi"), newAnswer("javajigi"));
question.canDelete(user, answers);
}
@Test(expected = CannotDeleteException.class)
public void canDelete_๋ค๋ฅธ_์ฌ์ฉ์_๋ต๋ณ() throws Exception {
User user = newUser("javajigi");
Question question = newQuestion("javajigi");
List<Answer> answers = Arrays.asList(newAnswer("javajigi"), newAnswer("sanjigi"));
question.canDelete(user, answers);
}
}
- ํ
์คํธ ์ฝ๋๋ฅผ ๋ณด๋ฉด,
QuestionDao,AnswerDao์ ๋ํ ์์กด๊ด๊ณ๊ฐ ์๊ธฐ์Mockํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ ํ์๋ ์์ผ๋ฉฐ, ๊ตฌํ ๋ํ ๊ฐ๋จํ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด์ QnaService ์ ๋ํ ํ
์คํธ๋ฅผ ์งํํ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํ ์ ์๋ค. ์ด๋ฏธ ํต์ฌ ๋น์ฆ๋์ค ๋ก์ง์ QuestionTest ์์ ๋ชจ๋ ๋๋ธ ์ํ์ด๋ค. ์ฆ, QnaServiceTest ์์๋ QnaService ๋ก์ง์ ๋ํด์๋ง ํ
์คํธ๋ฅผ ์ํํ๋ฉด ๋๋ค.
@RunWith(MockitoJUnitRunner.class)
public class QnaServiceTest {
@Mock
private QuestionDao questionDao;
@Mock
private AnswerDao answerDao;
private QnaService qnaService;
@Before
public void setup() {
qnaService = new QnaService(questionDao, answerDao);
}
@Test(expected = CannotDeleteException.class)
public void deleteQuestion_์๋_์ง๋ฌธ() throws Exception {
when(questionDao.findById(1L)).thenReturn(null);
qnaService.deleteQuestion(1L, newUser("userId"));
}
@Test
public void deleteQuestion_์ญ์ ํ ์_์์() throws Exception {
User user = newUser("userId");
Question question = new Question(1L, user.getUserId(), "title", "contents", new Date(), 0) {
public boolean canDelete(User user, List<Answer> answers) throws CannotDeleteException {
return true;
};
};
when(questionDao.findById(1L)).thenReturn(question);
qnaService.deleteQuestion(1L, newUser("userId"));
verify(questionDao).delete(question.getQuestionId());
}
@Test(expected = CannotDeleteException.class)
public void deleteQuestion_์ญ์ ํ ์_์์() throws Exception {
User user = newUser("userId");
Question question = new Question(1L, user.getUserId(), "title", "contents", new Date(), 0) {
public boolean canDelete(User user, List<Answer> answers) throws CannotDeleteException {
throw new CannotDeleteException("์ญ์ ํ ์ ์์.");
};
};
when(questionDao.findById(1L)).thenReturn(question);
qnaService.deleteQuestion(1L, newUser("userId"));
}
}
๊ด๊ณํ DB๋ฅผ ์ฌ์ฉํ๋ฉด์ ์ข ๋ ๊ฐ์ฒด์งํฅ์ ์ธ ๊ฐ๋ฐ์ ์ํด์๋ ORM(Object-Relational Mapping) ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์๋ค. ํ์ง๋ง ์ด๋ฅผ ์ฌ์ฉํ์ง ์์๋ ๋ฉ์๋์ ์ธ์๋ก ๊ฐ์ฒด๋ฅผ ์ ๋ฌํจ์ผ๋ก์จ ์ผ์ ๋ถ๋ถ ๊ฐ์ฒด์งํฅ์ ์ธ ๊ฐ๋ฐ์ด ๊ฐ๋ฅํ๋ค.
๐ฉ DI ํ๋ ์์ํฌ ์ค์ต
๐ง ์๊ตฌ์ฌํญ
์ง๋ฌธ/๋ต๋ณ ๊ธฐ๋ฅ์ DI๋ฅผ ์ ์ฉํ๋ ์์
์ LegacyHandlerMapping ์์ ๊ตฌํํ๋ค. DI๊ฐ ํ์ํ ๋๋ง๋ค ๋งค๋ฒ ์ง์ ์ธ์คํด์ค๋ฅผ ์์ฑํด ์ ๋ฌํ๊ธฐ ๊ท์ฐฎ์ผ๋ฏ๋ก, ์์ฒด์ ์ธ DI ํ๋ ์์ํฌ๋ฅผ ๊ตฌํํด๋ณธ๋ค.
์๋ก ๋ง๋ MVC ํ๋ ์์ํฌ๋ ์๋ฐ ๋ฆฌํ๋ ์
์ ํ์ฉํด @Controller annotation์ด ์ค์ ๋์ด ์๋ ํด๋์ค๋ฅผ ์ฐพ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ , URL ๋งคํ ์์
์ ์๋ํํ๋ค. ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ ํด๋์ค์ ๋ํ ์ธ์คํด์ค ์์ฑ ๋ฐ ์์กด๊ด๊ณ ์ค์ ์ ์๋ํํ๋ค.
-
๋จผ์ ๊ฐ ํด๋์ค ์ญํ ์ ๋ง๋๋ก
@Controller,@Service,@Repositoryannotation์ ์ค์ ํ๋ค.- 3๊ฐ์ ์ค์ ์ผ๋ก ์์ฑ๋ ๊ฐ ์ธ์คํด์ค ๊ฐ์ ์์กด๊ด๊ณ๋
@Injectannotation์ ์ฌ์ฉํ๋ค.
- 3๊ฐ์ ์ค์ ์ผ๋ก ์์ฑ๋ ๊ฐ ์ธ์คํด์ค ๊ฐ์ ์์กด๊ด๊ณ๋
-
DI ํ๋ ์์ํฌ๋ก ์์ฑ๋ ์ธ์คํด์ค์ ๊ฐ๋ฐ์๊ฐ
newํค์๋๋ก ์์ฑํ ์ธ์คํด์ค๋ฅผ ๋ถ๋ฆฌํ๋ค.- DI ํ๋ ์์ํฌ๋ฅผ ํตํด ์์ฑ๋ ์ธ์คํด์ค๋ ๋น(Bean)์ด๋ผ๊ณ ํ๋ค.
@Controller
public class QnaController extends AbstractController {
private MyQnaService qnaService;
@Inject
public QnaController(MyQnaService qnaService) {
this.qnaService = qnaService;
}
@RequestMapping("/questions")
public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
return jspView("/qna/list.jsp");
}
}
@Service
public class MyQnaService {
private UserRepository userRepository;
private QuestionRepository questionRepository;
@Inject
public MyQnaService(UserRepository userRepository, QuestionRepository questionRepository) {
this.userRepository = userRepository;
this.questionRepository = questionRepository;
}
...
}
@Repository
public class JdbcQuestionRepository implements QuestionRepository {
}
@Repository
public class JdbcUserRepository implements UserRepository {
}
- ์ ์ฝ์ฌํญ์ ๋น์
@Injectannotation์ ๊ฐ์ง๋ ์์ฑ์๋ ํ๋๋ง ์กด์ฌํ๋ฉฐ, ํด๋น annotation ์ค์ ์ด ๋์ด์์ง ์์ผ๋ฉด ์ธ์๊ฐ ์๋ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์ ๊ณตํ๋ ๊ฒ์ผ๋ก ํ๋ค.
๐ง 1๋จ๊ณ ํํธ
์ด ๋ฌธ์ ํด๊ฒฐ์ ์ํด์๋ ์ฌ๊ทํจ์๋ฅผ ์ฌ์ฉํด ๊ตฌํํ ์ ์๋ค. @Inject annotation์ด ์ค์ ๋ ์์ฑ์๋ฅผ ํตํด ๋น์ ์์ฑํด์ผ ํ๋ค.
- ๊ทธ๋ฐ๋ฐ ์ด ์์ฑ์์ ์ธ์๋ก ์ ๋ฌํ ๋น๋ ๋ค๋ฅธ ๋น๊ณผ ์์กด๊ด๊ณ๊ฐ ์๋ค. ์ด์ฒ๋ผ ๋น ๊ฐ์ ์์กด๊ด๊ณ๊ฐ ์ฐ๊ฒฐ๋์ด ์์ ์ ์๋๋ฐ, ์ด๋ ๋ค๋ฅธ ๋น๊ณผ ์์กด๊ด๊ณ๋ฅผ ๊ฐ์ง์ง ์๋ ๋น์ ์ฐพ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋๊น์ง ์ฌ๊ท๋ฅผ ์คํํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ ์ ์๋ค.
์ฌ๊ท๋ฅผ ํตํด ์์ฑํ ๋น์ BeanFactory ์ Map<Class<?>, Object> ์ ์ถ๊ฐํด ๊ด๋ฆฌํ๋ค.
- ์ธ์คํด์ค ์์ฑ ์ ๋จผ์
Class<?>์ ํด๋นํ๋ ๋น์ดMap<Class<?>, Object>์ ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ ํ ์์ฑํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ค. - ์ผ๋ฐ์ ์ธ ์บ์์ ๋์ ์๋ฆฌ์ ๊ฐ์ ์ฌ์ฌ์ฉ ๋ฐฉ๋ฒ์ด๋ค.
๐ง 2๋จ๊ณ ํํธ
๋น ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ธฐ ์ํ ์ฌ๊ท ํจ์ ์ง์์ ์ํด Class์ ๋ํ ๋น ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๋ฉ์๋์ ์์ฑ์์ ๋ํ ๋น ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๋ฉ์๋๊ฐ ํ์ํ๋ค.
private Object instantiateClass(Class<?> clazz) {
...
return null;
}
private Object instantiateConstructor(Constructor<?> constructor) {
...
return null;
}
- ์์์
instantiateClass()์ด๋ค. @Injectannotation์ด ์ค์ ๋ ์์ฑ์๊ฐ ์กด์ฌํ๋ฉดinstantiateConstructor()๋ฉ์๋๋ฅผ ํตํด ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ , ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ์์ฑ์๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค.instantiateConstructor()๋ฉ์๋๋ ์์ฑ์์ ์ธ์๋ก ์ ๋ฌํ ๋น์ด ์์ฑ๋์ดMap<Class<?>, Object>์ ์ด๋ฏธ ์กด์ฌํ๋ฉด ํด๋น ๋น์ ํ์ฉํ๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉดinstantiateClass()์ ํตํด ์๋ก์ด ๋น์ ์์ฑํ๋ค.
private Object instantiateConstructor(Constructor<?> constructor) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
List<Object> args = Lists.newArrayList();
for (Class<?> clazz : parameterTypes) {
...
args.add(bean);
}
return BeanUtils.instantiateClass(constructor, args.toArray());
}
๐ง ์ถ๊ฐ ์๊ตฌ์ฌํญ ๋ฐ ํํธ
@Controller ๊ฐ ์ค์ ๋์ด ์๋ ํด๋์ค๋ฅผ ์ฐพ๋ ControllerScanner ๋ฅผ DI ํ๋ ์์ํฌ๊ฐ ์๋ ํจํค์ง๋ก ์ด๋ํด @Controller , @Service , @Repository ์ ๋ํ ์ง์์ด ๊ฐ๋ฅํ๋๋ก ๊ฐ์ ํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ค๋ฅธ annotation๊น์ง ์ง์์ด ๊ฐ๋ฅํ๊ฒ ๋๋ฏ๋ก, BeanScanner ๋ก renameํ๋ค.
- ๋ํ
AnnotationHandlerMapping์ดBeanFactory์BeanScanner๋ฅผ ํ์ฉํ๋๋ก ๋ฆฌํฉํ ๋งํ๋ค.
๐ฉ DI ํ๋ ์์ํฌ ๊ตฌํ
BeanFactory ์ initialize() ๋ฉ์๋๋ฅผ ๊ตฌํํ๋ค.
public class BeanFactory {
private static final Logger logger = LoggerFactory.getLogger(BeanFactory.class);
private Set<Class<?>> preInstanticateBeans;
private Map<Class<?>, Object> beans = Maps.newHashMap();
public BeanFactory(Set<Class<?>> preInstanticateBeans) {
this.preInstanticateBeans = preInstanticateBeans;
}
@SuppressWarnings("unchecked")
public <T> T getBean(Class<T> requiredType) {
return (T) beans.get(requiredType);
}
public void initialize() {
for (Class<?> clazz : preInstanticateBeans) {
if (beans.get(clazz) == null) {
instantiateClass(clazz);
}
}
}
private Object instantiateClass(Class<?> clazz) {
Object bean = beans.get(clazz);
if (bean != null) {
return bean;
}
Constructor<?> injectedConstructor = BeanFactoryUtils.getInjectedConstructor(clazz);
if (injectedConstructor == null) {
bean = BeanUtils.instantiate(clazz);
beans.put(clazz, bean);
return bean;
}
logger.debug("Constructor : {}", injectedConstructor);
bean = instantiateConstructor(injectedConstructor);
beans.put(clazz, bean);
return bean;
}
private Object instantiateConstructor(Constructor<?> constructor) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
List<Object> args = Lists.newArrayList();
for (Class<?> clazz : parameterTypes) {
Class<?> concreteClazz = BeanFactoryUtils.findConcreteClass(clazz, preInstanticateBeans);
if (!preInstanticateBeans.contains(concreteClazz)) {
throw new IllegalStateException(clazz + "๋ Bean์ด ์๋๋๋ค.");
}
Object bean = beans.get(concreteClazz);
if (bean == null) {
bean = instantiateClass(concreteClazz);
}
args.add(bean);
}
return BeanUtils.instantiateClass(constructor, args.toArray());
}
}
BeanFactory์์ ํต์ฌ ๊ตฌํ ๋ก์ง์instantiateClass(),instantiateConstructor()๋ฉ์๋์ด๋ค. ์ด ๋ ๋ฉ์๋์ ์ฌ๊ทํธ์ถ์ ํตํด ๋ณต์กํ ์์กด๊ด๊ณ์ ์๋ ๋น์ ์์ฑํ๋ ๊ณผ์ ์ ์๋ฃํ ์ ์๋ค.
์ด์ @Controller , @Service , @Repository annotation์ด ์ค์ ๋ ๋น์ ์ฐพ๋ BeanScanner ๋ฅผ ๊ตฌํํ๋ค.
public class BeanScanner {
private Reflections reflections;
public BeanScanner(Object... basePackage) {
reflections = new Reflections(basePackage);
}
public Set<Class<?>> scan() {
return getTypeAnnotatedWith(Controller.class, Service.class, Repository.class);
}
private Set<Class<?>> getTypeAnnotatedWith(Class<? extends Annotation>... annotations) {
Set<Class<?>> preInstantiatedBeans = Sets.newHashSet();
for (Class<? extends Annotation> annotation : annotations) {
preInstantiatedBeans.addAll(reflections.getTypesAnnotatedWith(annotation));
}
return preInstantiatedBeans;
}
}
์ด์ ์์ฑํ BeanFactory ๋ฅผ AnnotationHandlerMapping ์ด ์ฌ์ฉํ๋๋ก ๋ฆฌํฉํ ๋งํ๋ค. ๋จผ์ @Controller annotation์ ์ค์ ํ ๋น์ ์กฐํํ๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ค.
public class BeanFactory {
...
public Map<Class<?>, Object> getControllers() {
Map<Class<?>, Object> controllers = Maps.newHashMap();
for (Class<?> clazz : preInstanticateBeans) {
Annotation annotation = clazz.getAnnotation(Controller.class);
if (annotation != null) {
controllers.put(clazz, annotation);
}
}
return controllers;
}
}
AnnotationHandlerMapping ์์๋ BeanFactory ๋ฅผ ์ด๊ธฐํํ ํ @Controller ๋ก ์ค์ ํ ๋น์ ์ฌ์ฉํ๋ฉด ๋๋ค.
public class AnnotationHandlerMapping implements HandlerMapping {
private static final Logger logger = LoggerFactory.getLogger(AnnotationHandlerMapping.class);
private Object[] basePackage;
private Map<HandlerKey, HandlerExecution> handlerExecutions = Maps.newHashMap();
public AnnotationHandlerMapping(Object... basePackage) {
this.basePackage = basePackage;
}
public void initialize() {
BeanScanner beanScanner = new BeanScanner(basePackage);
BeanFactory beanFactory = new BeanFactory(beanScanner.scan());
beanFactory.initialize();
Map<Class<?>, Object> controllers = beanFactory.getControllers();
Set<Method> methods = getRequestMappingMethods(controllers.keySet());
for (Method method : methods) {
RequestMapping rm = method.getAnnotation(RequestMapping.class);
logger.debug("register handlerExecution : url is {}, method is {}", rm.value(), method);
handlerExecutions.put(createHandlerKey(rm),
new HandlerExecution(controllers.get(method.getDeclaringClass()), method));
}
logger.info("Initialized AnnotationHandlerMapping!");
}
...
}
๐ ์ถ์ฒ
์๋ฐ ์น ํ๋ก๊ทธ๋๋ฐ Next Step : ํ๋์ฉ ๋ฒ๊ฒจ๊ฐ๋ ์ํ๊ป์ง ํ์ต๋ฒ - ๋ฐ์ฌ์ฑ